﻿using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomEditor(typeof(SimpleEditor))]
public class SimpleEditorInspector : Editor {

	private bool myListFold = false;
	private bool myArrayFloatFold = false;
	private bool myArrayVectorFold = false;

	public override void OnInspectorGUI ()
	{
		// target is SimpleEditor
		SimpleEditor simpleEditor = target as SimpleEditor;
		simpleEditor.showButton = EditorGUILayout.Toggle ("Show Button",simpleEditor.showButton);

		if (simpleEditor.showButton == true) GUILayout.Button("Show Button");

		simpleEditor.age = EditorGUILayout.IntField ("Age", simpleEditor.age);
		simpleEditor.height = EditorGUILayout.FloatField ("Float", simpleEditor.height);
		simpleEditor.name = EditorGUILayout.TextField ("Name", simpleEditor.name);
		simpleEditor.bloodType = (BLOODTYPE)EditorGUILayout.EnumPopup ("Blood Type",simpleEditor.bloodType);

		GUI.backgroundColor = (simpleEditor.cameraObject != null) ? Color.green : Color.red;

		simpleEditor.cameraObject = (GameObject)EditorGUILayout.ObjectField ("Camera Object", 
		                                                                     simpleEditor.cameraObject, 
		                                                                     typeof(GameObject), true);

		GUI.backgroundColor = Color.white;
		GUI.backgroundColor = (simpleEditor.myTransform != null) ? Color.green : Color.red;

		simpleEditor.myTransform = (Transform)EditorGUILayout.ObjectField ("My Transform",
		                                                                   simpleEditor.myTransform,
		                                                                   typeof(Transform), true);
		GUI.backgroundColor = Color.white;
		DrawSeparator (Color.black);

		if (myListFold = EditorGUILayout.Foldout (myListFold, "List")) {
			for (int i=0; i<simpleEditor.myList.Count; i++) {
				string filedName = "My List " + i;
				simpleEditor.myList[i] = EditorGUILayout.IntField(filedName,simpleEditor.myList[i]);
			}
		};

		DrawSeparator (Color.magenta);

		if(myArrayFloatFold = EditorGUILayout.Foldout(myArrayFloatFold, "Array Float")){
			for(int i=0; i<simpleEditor.arrayFloat.Length; i++){
				string fieldName = "Array Float " + i;
				simpleEditor.arrayFloat[i] = EditorGUILayout.FloatField(fieldName,simpleEditor.arrayFloat[i]);
			}
		};

		DrawSeparator (new Color(0.8f, 0.4f, 0.8f));

		if (myArrayVectorFold = EditorGUILayout.Foldout (myArrayVectorFold, "Array Vector")) {
			for (int i=0; i<simpleEditor.arrayVector.Length; i++) {
				string fieldName = "Array Vector " + i;
				simpleEditor.arrayVector[i] = EditorGUILayout.Vector3Field(fieldName, simpleEditor.arrayVector[i]);
			}
		};
	}

	void DrawSeparator (Color color){
		EditorGUILayout.Space ();
		Texture2D tex = new Texture2D (1, 1);
		GUI.color = color;
		float y = GUILayoutUtility.GetLastRect ().yMax;
		GUI.DrawTexture (new Rect (0.0f, y, Screen.width, 1.0f), tex);
		tex.hideFlags = HideFlags.DontSave;
		GUI.color = Color.white;
		EditorGUILayout.Space ();
	}
}












